package com.guodu.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.MaterialGhyctjDao;
import com.guodu.entity.MaterialGhyctj;
import com.guodu.entity.MaterialghyctjStatus;
import com.guodu.entity.Units;
import com.guodu.entity.ViewMaterialGhyc;
import com.guodu.vo.ViewVo;

@Repository("materialGhyctjDao")
public class MaterialGhyctjDaoImpl extends BaseDao implements MaterialGhyctjDao {

	@Override
	public void add(MaterialGhyctj entity) {
		// TODO Auto-generated method stub
		entity.setTjId("1");
		super.save(entity);

	}
	
	

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		super.delete(MaterialGhyctj.class, id);
	}

	@Override
	public void update(MaterialGhyctj entity) {
		// TODO Auto-generated method stub
		super.update(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialGhyctj> findByVo(ViewVo viewVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		hql.append("from MaterialGhyctj where 1=1");
		if(viewVo!=null){
			if(viewVo.getFunitsId()!=null){
				hql.append(" and funitsId = "+viewVo.getFunitsId());
			}
			if(viewVo.getUnitsId()!=null){
				hql.append(" and unitsId = "+viewVo.getUnitsId());
			}
			if(viewVo.getTime()!=null&&viewVo.getTime().trim().length()>0){
				hql.append(" and month = '"+viewVo.getTime()+"'");
			}
			if(viewVo.getStatus()!=null&&viewVo.getStatus()!=5){
				hql.append(" and status = "+viewVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}
		return super.find(hql.toString());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ViewMaterialGhyc> findViewByVo(ViewVo viewVo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		hql.append("from ViewMaterialGhyc where 1=1");
		if(viewVo!=null){
			if(viewVo.getFunitsId()!=null){
				hql.append(" and funitsId = "+viewVo.getFunitsId());
			}
			if(viewVo.getUnitsId()!=null){
				hql.append(" and unitsId = "+viewVo.getUnitsId());
			}
			if(viewVo.getTime()!=null&&viewVo.getTime().trim().length()>0){
				hql.append(" and month = '"+viewVo.getTime()+"'");
			}
		}
		return super.find(hql.toString());
	}

	@Override
	public boolean isExist(Long unitsId, String month) {
		// TODO Auto-generated method stub
		String hql = "from MaterialGhyctj where unitsId = "+unitsId+" and month= '"+month+"'";
		if(super.find(hql).size()>0){
		return true;
		}else{
			return false;
		}
	}



	@Override
	public MaterialGhyctj findById(String id) {
		// TODO Auto-generated method stub
		return (MaterialGhyctj) super.findById(MaterialGhyctj.class, id);
	}



	@Override
	public void changeStatus(List<MaterialghyctjStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}



	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialghyctjStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return super.findByProperty(MaterialghyctjStatus.class, "materialId", id);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialGhyctj set status =");
		hql.append(status);
		hql.append(" where tjId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

}
